package insight;

//抛出异常来拒绝任务
public class ExceptionRejuctHandle implements RejectHandle{
    @Override
    public void reject(Runnable command, MyThreadPool pool) {
        // 获取当前线程名
        String currentThread = Thread.currentThread().getName();

        // 获取核心线程和额外线程的状态
        String coreThreadsStatus = "CoreThreads: " + pool.getCoreThreadsList().size() + "/" + pool.getCorePoolSize();
        String extraThreadsStatus = "ExtraThreads: " + pool.getExtraThreadsList().size() + "/" + (pool.getMaximumPoolSize() - pool.getCorePoolSize());

        // 获取阻塞队列状态
        String queueStatus = "Queue: " + pool.getBlockingQueue().size() + "/" + pool.getBlockingQueue().remainingCapacity();

        // 构造详细错误信息
        String errorMsg = String.format(
                "任务被拒绝！\n" +
                        "当前线程: %s\n" +
                        "线程池状态: %s, %s\n" +
                        "阻塞队列状态: %s",
                currentThread, coreThreadsStatus, extraThreadsStatus, queueStatus
        );
        System.err.println(errorMsg);//输出错误信息,直接抛异常会导致主线程停止
        //throw new RuntimeException(errorMsg);
    }
}
